package com.ruoyi.project.system.controller;

import java.util.List;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ruoyi.project.system.service.ICityTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.SysCity;
import com.ruoyi.project.system.service.ISysCityService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;

import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 城市Controller
 *
 * @author jjp
 * @date 2020-04-09
 */
@RestController
@RequestMapping("/system/city")
public class SysCityController extends BaseController {
    @Autowired
    private ISysCityService sysCityService;

    /**
     * 查询城市列表
     */
    @PreAuthorize("@ss.hasPermi('system:city:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysCity sysCity) {
        startPage();
        List<SysCity> list = sysCityService.selectSysCityList(sysCity);
        return getDataTable(sysCityService.buildTypeTree(list));
    }

    /**
     * 获取城市详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:city:query')")
    @GetMapping(value = "/cityMsg/{cityId}")
    public AjaxResult getInfo(@PathVariable("cityId") Long cityId)
    {
        AjaxResult success = AjaxResult.success(sysCityService.selectSysCityById(cityId));
        return AjaxResult.success(success);
    }


    /**
     * 新增城市
     */
    @PreAuthorize("@ss.hasPermi('system:city:add')")
    @Log(title = "城市", businessType = BusinessType.INSERT)
    @PostMapping("/addNewCity")
    public AjaxResult add(@RequestBody SysCity sysCity)
    {
        List<SysCity> sysCities = sysCityService.queryByCityName(sysCity);
        if(sysCities.size() != 0){
            return AjaxResult.error("不可重复添加");
        }
        return toAjax(sysCityService.insertSysCity(sysCity));
    }

    /**
     * 修改城市
     */
    @PreAuthorize("@ss.hasPermi('system:city:edit')")
    @Log(title = "城市", businessType = BusinessType.UPDATE)
    @PutMapping("/updateCity")
    public AjaxResult edit(@RequestBody SysCity sysCity)
    {
        System.out.println(sysCity.getCityName());
        return toAjax(sysCityService.updateSysCity(sysCity));
    }

    /**
     * 删除城市
     */
    @PreAuthorize("@ss.hasPermi('system:city:remove')")
    @Log(title = "城市", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteCity/{cityIds}")
    public AjaxResult remove(@PathVariable Long[] cityIds)
    {
        return toAjax(sysCityService.deleteSysCityByIds(cityIds));
    }

    /**
     * 通过城市查服务
     */
    @PostMapping("/cityService")
    public AjaxResult CityService(@RequestBody SysCity sysCity) {
        List<SysCity> sysCities = sysCityService.QueryCityService(sysCity.getCityId());
        return AjaxResult.success(sysCities);
    }
}
